package com.suray.basic.wcs.plc.pojo.oper.single;

import com.suray.basic.wcs.plc.config.resolver.PlcAnnotation;
import com.suray.basic.wcs.plc.core.PlcOffset;
import com.suray.basic.wcs.plc.enums.Location;
import com.suray.basic.wcs.plc.enums.PlcDeviceType;
import com.suray.basic.wcs.utils.Coord;
import com.suray.basic.wcs.utils.LoggerUtil;

import java.io.IOException;

/**
 * 单体输送线
 */
public class MonomerConveyorOper extends ShapeConveyorOper implements DeviceInterface {

    //提升机通讯中断
    private static final PlcOffset LIFT_COMM_ERROR_1 = new PlcOffset(62, null);
    private static final PlcOffset LIFT_COMM_ERROR_2 = new PlcOffset(63, null);
    private static final PlcOffset TRANS_CARGO_CMD_READ = new PlcOffset(72, null);

    /**
     * 输送机调整指令
     * 1：正转（1方向），2：反转（3方向），0：无效
     */
    private static final int TRANS_CARGO_CMD = 72;

    public MonomerConveyorOper(PlcDeviceType plcDeviceType, int startNo, String deviceName, int deviceNo, Coord coord,
                               Location location, Coord lockLocation, long deviceId, Integer gridzMax,
                               Integer gridzRelative, int plcId) {
        super(plcDeviceType, startNo, deviceName, deviceNo, coord, location, lockLocation, deviceId, gridzMax,
                gridzRelative, plcId);
    }

    public int getTransCargoCmdRead() {
        return readPlc(TRANS_CARGO_CMD_READ);
    }

    public int getLiftCommError1() {
        return readPlc(LIFT_COMM_ERROR_1);
    }

    public int getLiftCommError2() {
        return readPlc(LIFT_COMM_ERROR_2);
    }

    public Integer getDeviceType() {
        Integer deviceType = super.getDeviceType();
        if (deviceType == null) {
            return null;
        }

        if (deviceType.equals("3") ) {
            return 1;
        }
        return deviceType;
    }

    @Override
    public boolean inCargoSignal() {
        return super.isInCargoSignalValid();
    }

    @Override
    public boolean outCargoSignal() {
        return super.isOutCargoSignalValid();
    }

    @Override
    public boolean permitRun() {
        return super.isControl() && !super.isRunning();
    }

    @Override
    public boolean canControl() {
        return super.isControl();
    }

    @Override
    public boolean isError() {
        return super.isErrorState();
    }

    @Override
    public Integer errorCode() {
        return super.getErrorInfo();
    }

    /**
     * 输送线转动
     * @param forwardRun : true(正传), false(反转)
     */
    @PlcAnnotation(value = "输送线转动")
    public void transCargo(boolean forwardRun) throws IOException {
        LoggerUtil.info("输送线转动，设备：" + getDeviceName() + ", 正传:" + forwardRun, this.getClass());
        oper("输送线转动", this.getClass(), TRANS_CARGO_CMD, 10, forwardRun ? 1 : 2);
    }

    /**
     * 输送线转动是否完成
     * 正传是1号光电到2号光电，反转是2号光电到1号光电
     * @param forwardRun : 正传
     */
    public boolean isTransCargoEnd(boolean forwardRun) {
        if (forwardRun) {
            if (isInCargoSignalValid() && !isRunning()) {
                if (getTransCargoCmdRead() != 0) {
                    setDeviceStatus("等待输送线转动完成");
                    return false;
                }
                return true;
            } else {
                setDeviceStatus("等待入库光电有货");
            }
        } else {
            if (isOutCargoSignalValid() && !isRunning()) {
                if (getTransCargoCmdRead() != 0) {
                    setDeviceStatus("等待输送线转动完成");
                    return false;
                }
                return true;
            } else {
                setDeviceStatus("等待出库光电有货");
            }
        }
        return false;
    }

    /**
     * 清除输送线转动完成信号
     */
    @PlcAnnotation(value = "清除输送线转动完成信号")
    public void clearTransEndSig() {

    }
}
